#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a backup of the Bacula build directory on two Volumes
#   then copy it to another device.             
#
# This script uses the virtual disk autochanger and migration scripts
#
TestName="copy-uncopied-test"
JobName=MigVolBackup
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
echo "${cwd}/build" >${cwd}/tmp/file-list
#cp ${cwd}/bin/bacula-sd.conf ${cwd}/tmp/1
#sed "s%# Maximum File Size%  Maximum File Size%" ${cwd}/tmp/1 >${cwd}/bin/bacula-sd.conf

change_jobname NightlySave $JobName
sed 's/migrate/copy/g' ${cwd}/bin/bacula-dir.conf > ${cwd}/tmp/1
sed 's/Migrate/Copy/g' ${cwd}/tmp/1 > ${cwd}/bin/bacula-dir.conf
rm -f ${cwd}/build/po/test.po >/dev/null

touch ${cwd}/tmp/log2.out

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Allow Duplicate Jobs", "no", "Job", "MigVolBackup")'

# Add this for bug #6637 verification. Pool setting should not be overriden by settings below
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "FullBackupPool", "Scratch", "Job", "copy-uncopied")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "IncrementalBackupPool", "Scratch", "Job", "copy-uncopied")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "DifferentialBackupPool", "Scratch", "Job", "copy-uncopied")'

start_test

#
# Note, we first backup into Pool Default, 
#          then Copy into Pool Full. 
#              Pool Default uses Storage=File
#              Pool Full    uses Storage=DiskChanger

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=FileVolume001 Pool=Default
label storage=File volume=FileVolume002 Pool=Scratch
label storage=DiskChanger volume=ChangerVolume001 slot=1 Pool=Scratch drive=0
label storage=DiskChanger volume=ChangerVolume002 slot=2 Pool=Scratch drive=0
@# 
run job=$JobName level=Full yes
wait
run job=$JobName level=Full yes
wait
update volume=FileVolume001 VolStatus=Used
run job=$JobName level=Full yes
wait
messages
@$out ${cwd}/tmp/log11.out
@############################################################
@# should copy job 1,2,3
@# Expect: 3 copies (1,2,3)
@############################################################
setdebug level=200 dir trace=1
run job=copy-uncopied yes
@sleep 5 
wait
messages
list jobs
list volumes
list copies
@$out ${cwd}/tmp/log12.out
@############################################################
@# should not copy jobs
@# Expect: 3 copies (1,2,3)
@############################################################
run job=copy-uncopied yes
wait
messages
list copies
@$out ${cwd}/tmp/log13.out
@############################################################
@# should not have a copy of the copy
@# Expect: 2 copies (2,3)
@############################################################
delete jobid=1
run job=copy-uncopied yes
wait
messages
list copies
@$out ${cwd}/tmp/log14.out
@############################################################
@# should copy jobid=2
@# Expect: 2 copies (2,3)
@############################################################
delete jobid=8
run job=copy-uncopied yes
wait
messages
list copies
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File
stop_bacula

bstat=0
rstat=0
dstat=0
zstat=0

perl -Mscripts::functions -e 'check_multiple_copies(3)' tmp/log11.out
bstat=`expr $bstat + $?`

perl -Mscripts::functions -e 'check_multiple_copies(3)' tmp/log12.out
bstat=`expr $bstat + $?`

perl -Mscripts::functions -e 'check_multiple_copies(2)' tmp/log13.out
bstat=`expr $bstat + $?`

perl -Mscripts::functions -e 'check_multiple_copies(2)' tmp/log14.out
bstat=`expr $bstat + $?`

check_two_logs
end_test
